package myJs

import myJs.Tool._
import myJs.Utils._
import myJs.Implicits._
import myJs.myPkg._
import org.scalajs.dom.{Element, _}
import org.scalajs.jquery.jQuery
import scalatags.Text.all._

import scala.collection.mutable
import scala.scalajs.js
import scala.scalajs.js.JSConverters._
import scala.scalajs.js.annotation.{JSExport, JSExportTopLevel}
import myJs.myPkg.jquery._


/**
 * Created by yz on 2019/4/25
 */
@JSExportTopLevel("ReportTool")
object ReportTool {

  val time = 0

  @JSExport("init")
  def init = {

    val options = TableOptions.pagination(true).pageList(js.Array("10", "25", "50", "100", "all"))
    $(".MyTable").bootstrapTable(options)

    $(window).scroll { () =>
      scroll
    }

    toggle

    imgHover

    //    singleMenuClick

    leftClick


  }

  def leftClick = {
    $(".lanmu-list a").on("click", (y: Element, e: Event) => {
      $(".lanmu-list a").removeClass("current")
      $(y).addClass("current")
    })

  }

  def singleMenuClick = {
    $(".singleMenu").on("click", (y: Element, e: Event) => {
      $(".lanmu-list a,.title").removeClass("current")
      $(y).find(".title").addClass("current")
    })
  }

  def imgHover = {
    $(".myImg").on("mouseover", (y: Element, e: Event) => {
      val title = $(y).attr("alt").toString
      val pElem = $(y).parents(".pgwSlider").prev(".imgP")
      pElem.find("span").text(title)
    })

  }

  def singleScroll(id: String, currentTop: Int) = {
    val singleList = List(id)
    val execList = singleList.filter { mainId =>
      (currentTop - $(s"#${mainId}").offset().top) > 0
    }.toList.sortBy { mainId =>
      (currentTop - $(s"#${mainId}").offset().top)
    }.headOption

    execList.map { y =>
      val initOffset = $(s"#${y}").offset()
      if (currentTop > initOffset.top) {
        $(".lanmu-list a").removeClass("current")
        $(s".${y}").addClass("current")
      }
    }
  }

  def scroll = {
    val currentTop = $(window).scrollTop() + (145)
    singleScroll("项目简介", currentTop)
    val map = mutable.LinkedHashMap("summary" -> "leftSummary")
    val basicTs = $(".myBasic").mapElems { y =>
      val id = $(y).attr("id").toString
      val leftId = id.replace("Basic", "")
      (id, s"${leftId}")
    }
    map ++= basicTs

    val execMap = map.filter { case (mainId, leftId) =>
      (currentTop - $(s"#${mainId}").offset().top) > 0
    }.toList.sortBy { case (mainId, leftId) =>
      (currentTop - $(s"#${mainId}").offset().top)
    }.headOption
    execMap.map { case (mainId, leftId) =>
      val initOffset = $(s"#${mainId}").offset()
      if (currentTop > initOffset.top) {
        val topBElem = $(s"#${leftId}").parent().parent().parent().prev().find("a:first").find("b")
        val topBText = topBElem.text().trim
        if (topBText == "+") {
          topBElem.text("-")
          $(s"#${leftId}").parent().parent().parent().fadeIn(time)
        }
        val bElem = $(s"#${leftId}").find("b")
        val bText = bElem.text().trim
        if (bText == "+") {
          bElem.text("-")
          $(s"#${leftId}").parent().next().fadeIn(time)
          $(s"#${leftId}").parent().prev(":not('.singleMenu')").fadeOut(time)
          $(s"#${leftId}").parent().prev().prev().find("b").text("+")
          $(s"#${leftId}").parent().next().next().next().fadeOut(time)
          $(s"#${leftId}").parent().next().next().find("b").text("+")
        }
        $(".lanmu-list a,.title").removeClass("current")
        $(s"#${leftId}").parent().next().find("a:first").addClass("current")
      }
    }


    val onlyLightMap = mutable.LinkedHashMap[String, String]()
    val ts = $(".myMain").mapElems { y =>
      val id = $(y).attr("id").toString
      (id, s"${id}Left")
    }
    onlyLightMap ++= (ts)

    val onlyLightExecMap = onlyLightMap.filter { case (mainId, leftId) =>
      (currentTop - $(s"#${mainId}").offset().top) > 0
    }.toList.sortBy { case (mainId, leftId) =>
      (currentTop - $(s"#${mainId}").offset().top)
    }.headOption


    onlyLightExecMap.foreach { case (mainId, leftId) =>
      $(".lanmu-list a").removeClass("current")
      $(s"#${leftId}").addClass("current")
    }

    singleScroll("公司简介", currentTop)

  }


  def toggle = {
    $(".shouqi").on("click", (y: Element, e: Event) => {
      val bElem = $(y).find("b")
      val bStr = bElem.text().trim
      bStr match {
        case "+" => bElem.text("-")
          $(y).parent().next().fadeIn(time)
        case "-" => bElem.text("+")
          $(y).parent().next().fadeOut(time)
      }
    }
    )

    $(".shouqi_2").on("click", (y: Element, e: Event) => {
      val bElem = $(y).prev().find("b")
      val bStr = bElem.text().trim
      bStr match {
        case "+" => bElem.text("-")
          $(y).parent().next().show()
        case "-" => bElem.text("+")
          $(y).parent().next().hide()
      }
    })

    $("#zhankai").on("click", (y: Element, e: Event) => {
      val bElem = $(y).find("b")
      val bStr = bElem.text().trim
      bStr match {
        case "展开目录" => bElem.text("收起目录")
          $(".shouqi").find("b").text("-")
          $(".lanmu-content").show()
        case "收起目录" => bElem.text("展开目录")
          $(".shouqi").find("b").text("+")
          $(".lanmu-content").hide()
      }
    }
    )

    $("#kaiguan").on("click", (y: Element, e: Event) => {
      val bElem = $(y).find("b")
      val bStr = bElem.text().trim
      bStr match {
        case "<<" => bElem.text(">>")
          $("#zhankai").html("")
          $(".mulu").css("width", "66px")
          $("#zhankai").css("width", "0px")
          $("#main").css("width", "90%")
          $("#left").css("display", "none")
        case ">>" => bElem.text("<<")
          $("#zhankai").html("<b>展开目录</b>")
          $(".mulu").css("width", "20%")
          $("#main").css("width", "74%")
          $("#left").show()
      }
    }
    )


  }


}
